/**
 * uni.showToast 统一使用底部
 */
export function showToast(msg) {
	return uni.showToast({
		icon: "none",
		title: msg,
		position: "bottom"
	})
}
/**
 * 缓存用户信息
 */
export function setUserInfo(info) {
	return uni.setStorageSync('userInfo', info)
}
/**
 * 获取用户信息
 */
export function getUserInfo() {
	return uni.getStorageSync('userInfo')
}
/**
 * 获取用户token
 */
export function getUserToken() {
	let userInfo = getUserInfo()
	if (userInfo) {
		return userInfo.token
	} else {
		return ""
	}
}
/**
 * 获取用户ID
 */
export function getUserId() {
	let userInfo = getUserInfo()
	if (userInfo) {
		return userInfo.userId
	} else {
		return 0
	}
}

/**
 * 清理用户信息
 */
export function clearUserInfo() {
	uni.removeStorageSync("userInfo")
}

/**
 * 格式化：日期时间 yyyy-MM-dd hh:mm:ss
 * @param {Object} date
 */
export function formatDateTime(date) {
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0');
	const day = String(date.getDate()).padStart(2, '0');
	const hours = String(date.getHours()).padStart(2, '0');
	const minutes = String(date.getMinutes()).padStart(2, '0');
	const seconds = String(date.getSeconds()).padStart(2, '0');

	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

/**
 * 格式化：日期 yyyy-MM-dd
 * @param {Object} date
 */
export function formatDate(date) {
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0');
	const day = String(date.getDate()).padStart(2, '0');
	const hours = String(date.getHours()).padStart(2, '0');
	const minutes = String(date.getMinutes()).padStart(2, '0');
	const seconds = String(date.getSeconds()).padStart(2, '0');

	return `${year}-${month}-${day}`;
}

/**
 * 是否满18岁
 * 
 * @param {Object} date
 */
export function is18YearsOld(birthDateString) {
	// 将出生日期字符串转换为 Date 对象
	const birthDate = new Date(birthDateString);
	// 获取当前日期
	const currentDate = new Date();

	// 计算当前年份和出生年份的差值
	let age = currentDate.getFullYear() - birthDate.getFullYear();

	// 如果当前日期还没到今年的生日，则年龄减1
	if (
		currentDate.getMonth() < birthDate.getMonth() ||
		(currentDate.getMonth() === birthDate.getMonth() &&
			currentDate.getDate() < birthDate.getDate())
	) {
		age--;
	}

	// 判断是否满18岁
	return age >= 18;
}